﻿using System;
using System.Collections.Generic;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Linq;
using gpuHough.DirectX;

namespace gpuHough
{
    abstract class Rect<VertexType> : Shape<VertexType> 
        where VertexType : struct
    {
        protected readonly Vector2[] CornerPositions = { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) };
        protected const int VerticesCount = 6;

        public enum corners 
        {
            topLeft = 0,
            topRight,
            bottomRight,
            bottomLeft             
        }

        private Vector2 size;
        private Vector2 position;
        
        public Rect(Vector2 size, Vector2 position)
        {
            this.size = size;
            this.position = position;            
        }

        public override VertexType[] BuildShape()
        {   
            //vertices
            VertexType[] cornerVertices = new VertexType[CornerPositions.Length];

            for (int corner = 0; corner < CornerPositions.Length; corner++) 
            {   
                Vector3 cornerPosition = Vector2ToVector3(position + new Vector2(size.X * CornerPositions[corner].X, size.Y * CornerPositions[corner].Y));
                cornerVertices[corner] = BuildRectVertex(cornerPosition, corner);
            };

            //triangles            
            var trianglesVertices = new VertexType[VerticesCount];

            trianglesVertices[0] = cornerVertices[(int)corners.topLeft];
            trianglesVertices[1] = cornerVertices[(int)corners.bottomLeft];
            trianglesVertices[2] = cornerVertices[(int)corners.topRight];

            trianglesVertices[3] = cornerVertices[(int)corners.topRight];
            trianglesVertices[4] = cornerVertices[(int)corners.bottomLeft];
            trianglesVertices[5] = cornerVertices[(int)corners.bottomRight];

            return trianglesVertices;
        }

        protected abstract VertexType BuildRectVertex(Vector3 cornerPosition, int cornerIndex);
    }
}
